package _5_MoNi;

public class A29_spiralOrder {
    public  int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0) return new int[0];
        int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, x = 0;
        int[] res = new int[(r + 1) * (b + 1)];
        while(true) {
            for(int i = l; i <= r; i++) res[x++] = matrix[t][i]; // left to right.
            if(++t > b) break;
            for(int i = t; i <= b; i++) res[x++] = matrix[i][r]; // top to bottom.
            if(l > --r) break;
            for(int i = r; i >= l; i--) res[x++] = matrix[b][i]; // right to left.
            if(t > --b) break;
            for(int i = b; i >= t; i--) res[x++] = matrix[i][l]; // bottom to top.
            if(++l > r) break;
        }
        return res;
    }

    public static void main(String[] args) {
//        int[][] a = new int[10][10];
//        A29_spiralOrder a29_spiralOrder = new A29_spiralOrder();
//        a29_spiralOrder.spiralOrder(a);
        int x = 0;
        int[] ints = new int[3];
        ints[x++] = 1;
        for (int anInt : ints) {
            System.out.println(anInt);
        }
        int[] ints1 = new int[2 * 3];
    }

}
